<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" name="" id="ipt1" />:防抖输入框
    <input type="text" name="" id="ipt2" />:未防抖输入框
    <script>
      const oIpt1 = document.getElementById("ipt1");
      const oIpt2 = document.getElementById("ipt2");

      function center(e) {
        console.log(1);
      }

      oIpt1.onkeyup = debonce(center, 3000);
      oIpt2.onkeyup = function () {
        console.log(2);
      };

      function debonce(center, time) {
        let timer = null;
        return function (e) {
          //每次进来清除上一次的定时器
          clearTimeout(timer);
          timer = setTimeout(() => {
            center.call(this, e);
          }, time);
        };
      }
    </script>
  </body>
</html>
